package com.googlecode.tabletags.components;

import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.components.UIBean;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * Renders a cell in a table. (Maybe we should rename this to Cell?)
 */
@StrutsTag(name = "column", tldTagClass = "com.googlecode.tabletags.views.jsp.ui.ColumnTag", description = "Renders a cell in a table")
public class Column extends UIBean {
    private static final Log LOG = LogFactory.getLog(Column.class);

    public static final String COLUMN_OPEN_TEMPLATE = "table-colstart";
    public static final String COLUMN_TEMPLATE = "table-colend";

    // sort column attributes
    protected String sortKey;
    protected String sortable;

    /**
     * @param stack
     * @param request
     * @param response
     */
    public Column(ValueStack stack, HttpServletRequest request,
        HttpServletResponse response) {
        super(stack, request, response);
    }

    @Override
    public boolean start(Writer writer) {
        boolean evaluateBody = false;
        Table table = (Table) this.findAncestor(Table.class);
        if(table.getRowNum() < 0) {
            table.addColumn(this);
        }
        evaluateParams();
        try {
            if(table.getRowNum() >= 0) {
                mergeTemplate(writer, buildTemplateName(null,
                    this.COLUMN_OPEN_TEMPLATE));
                evaluateBody = true;
            }
        } catch(Exception e) {
            LOG.error("Could not open template", e);
            e.printStackTrace();
        }
        return evaluateBody;
    }

    @Override
    public boolean end(Writer writer, String body) {
        Table table = (Table) this.findAncestor(Table.class);
        evaluateParams();
        try {
            if(table.getRowNum() >= 0) {
                mergeTemplate(writer, buildTemplateName(null,
                    this.COLUMN_TEMPLATE));
            }
        } catch(Exception e) {
            LOG.error("error when rendering", e);
        } finally {
            popComponentStack();
        }

        return false;
    }

    protected String getDefaultTemplate() {
        return null; // not used by this tag
    }

    @StrutsTagAttribute(description = "sortable")
    public void setSortable(String sortable) {
        this.sortable = sortable;
    }

    @StrutsTagAttribute(description = "sortKey")
    public void setSortKey(String sortKey) {
        this.sortKey = sortKey;
    }

    @StrutsTagAttribute(description = "title")
    public void setTitle(String title) {
        super.setTitle(title);
    }

    public String getSortable() {
        return sortable;
    }

    public String getSortKey() {
        return sortKey;
    }
    
    public String getTitle() {
        return title;
    }
}
